package com.yyk.yykywd.mvvm.ui

import android.graphics.BitmapFactory
import android.os.Handler
import android.os.Message
import com.yyk.yykywd.IMG
import com.yyk.yykywd.R
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.util.FileUtils
import com.yyk.yykywd.util.GlideUtils
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_pic_detail.*
import java.io.InputStream
import java.net.URL


class PicDetailActivity : BaseActivity() {

    private val handler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            ToastUtil.showShort("图片保存成功")
        }
    }

    override fun initData() {
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_pic_detail
    }

    override fun initView() {
        val img = intent.getStringExtra(IMG)
        setBackArrow()
        setTitle("图片详情")
        setMainTitleRightText("保存") {
            Thread(Runnable {
                val url = URL(img)
                val inputStream: InputStream = url.openStream()
                val bitmap = BitmapFactory.decodeStream(inputStream)
                val file = FileUtils.saveBitmapToFile(bitmap)
                FileUtils.insertImage(applicationContext, file)
                handler.sendEmptyMessage(1)
            }).start()
        }

//        val dm = DisplayMetrics()
//        windowManager.defaultDisplay.getMetrics(dm)
//        val width: Int = dm.widthPixels
//        val lp: ViewGroup.LayoutParams = iv.layoutParams
//        lp.width = width
//        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT
//        iv.layoutParams = lp
//        iv.maxWidth = width
//        // 这里其实可以根据需求而定，我这里测试为最大宽度的1.5倍
//        iv.maxHeight = width * 5


        GlideUtils.loadImage(mActivity, img, iv)
    }

}
